<template>
  <div class="role">
    <SearchPage
      :search-from-config="searchFromConfig"
      @ResetClick="ResetClick"
      @QueryClick="QueryClick"
    ></SearchPage>
    <ContentPage
      ref="contentRef"
      :content-table-config="contentTableConfig"
      page-name="role"
      @insertNewClick="insertNewClick"
      @EditClick="EditClick"
    ></ContentPage>
    <modal-page
      pageName="role"
      :title="title"
      :modalConfig="modalConfig"
      ref="modalRef"
      :default-info="defaultInfo"
      :other-info="otherInfo"
    >
      <div class="menu-tree">
        <el-tree
          ref="treeRef"
          :data="allMenu"
          show-checkbox
          node-key="id"
          :props="{ children: 'children', label: 'name' }"
          @check="hanlderCheckChange"
        />
      </div>
    </modal-page>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed, nextTick } from 'vue'
import ContentPage from '@/components/content-page'
import { contentTableConfig } from './config/content.config'
import { searchFromConfig } from './config/search.config'
import ModalPage from '@/components/modal-page/src/modal-page.vue'
import { modalConfig } from './config/modal.config'
import { useModalPage } from '@/hooks/useModalPage'
import { useStore } from '@/store'
import { getMenuLeafKeys } from '@/utils/MenusToRoutes'
import { ElTree } from 'element-plus'
import { useSearchPage } from '@/hooks/useSearchPage'
export default defineComponent({
  components: {
    // SearchPage,
    ContentPage,
    ModalPage
  },
  name: 'role',
  setup() {
    // 对modal的处理
    const title = ref('')
    const insertNewCallBack = () => {
      title.value = '新建角色'
    }

    const treeRef = ref<InstanceType<typeof ElTree>>()

    const editCallBack = (item: any) => {
      title.value = '编辑角色'
      const leafKeys = getMenuLeafKeys(item.menuList)
      nextTick(() => {
        treeRef.value?.setCheckedKeys(leafKeys, false)
      })
    }
    const { modalRef, EditClick, insertNewClick, defaultInfo } = useModalPage(
      insertNewCallBack,
      editCallBack
    )
    // 对allmenu的处理 el-tree
    const store = useStore()
    // 让数据具有响应式
    const allMenu = computed(() => store.state.allMenu)

    // 对 modal的 menu-tree的扩展
    const otherInfo = ref({})
    const hanlderCheckChange = (data1: any, data2: any) => {
      const checkedKeys = data2.checkedKeys
      const halfCheckedKeys = data2.halfCheckedKeys
      const menuList = [...checkedKeys, ...halfCheckedKeys]
      otherInfo.value = { menuList }
    }
    const { contentRef, ResetClick, QueryClick } = useSearchPage()

    return {
      contentTableConfig,
      searchFromConfig,
      title,
      insertNewClick,
      EditClick,
      modalConfig,
      modalRef,
      defaultInfo,
      allMenu,
      otherInfo,
      hanlderCheckChange,
      treeRef,
      contentRef,
      ResetClick,
      QueryClick
    }
  }
})
</script>

<style scoped lang="less">
.menu-tree {
  margin-left: 30px;
}
</style>
